//
//  IDGWBHistoryViewController.m
//  Goodman Web Browser
//
//  Created by iD Student on 7/11/13.
//  Copyright (c) 2013 Apple Inc. All rights reserved.
//

#import "IDGWBHistoryViewController.h"

#import "IDGWBViewController.h"


@interface IDGWBHistoryViewController ()

@end

@implementation IDGWBHistoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setClearHistoryButtonState];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return  [_arrayHistory count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    cell.textLabel.text = [_arrayHistory objectAtIndex: indexPath.item];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL *url = [NSURL URLWithString:[_arrayHistoryURLS objectAtIndex:indexPath.item]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webViewController setUrlToLoad:url];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void) setClearHistoryButtonState{
    
    
    [self.buttonClear setEnabled:[_arrayHistory count] > 0];
    
}
- (IBAction)buttonClear:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Clear History" message:@"Are you sure you want to clear your history? This cannot be undone." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Clear History", nil, nil];
    [alert show];
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    //You must change 0 to another integer value in the case that UIAlertView includes more than one button.
    
    if (buttonIndex == 0) {

        
        
    }
else
{
    _arrayHistory = [[NSArray alloc] init];
    _webViewController.setHistory = [[NSMutableSet alloc] init];
    _buttonBack = [[UIBarButtonItem alloc] init];
    [self setClearHistoryButtonState];
    [_tableView reloadData];
    
    
}
    
   
}

- (IBAction)buttonDismiss:(id)sender {
    
    [self.buttonBack setEnabled:NO];
    [_buttonBack setEnabled:NO];
    [self.buttonBack setEnabled:NO];
    [self.navigationController popViewControllerAnimated:YES];
    
}
@end
